<template>
  <div class="account-settings-info-view">

    <a-form :form="form">

      <a-table :columns="userColumns" :dataSource="businessUserList" :pagination="false" rowKey="id" :scroll="{ y: 300 }" bordered>
            <span slot="name" slot-scope="name,record">
              <img :src="record.face" style="width: 40px; vertical-align: middle;" /> {{record.name}}
            </span>
        <template slot="action" slot-scope="text,record,index">
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除此人员吗" @confirm="deleteUser(record,index)"  okText="确定" cancelText="取消">
            <a href="javascript:;">删除</a>
          </a-popconfirm>
        </template>
        <template slot="footer" slot-scope="currentPageData">
          <user-picker style="width: 200px; margin-right: 10px;" :formData="userFormData" field="user_id" />
          <a-select  style="width: 200px; margin-right: 10px;"  v-model="userFormData.type" placeholder="角色类型"  >
            <a-select-option value="管理员">管理员</a-select-option>
            <a-select-option value="店员">店员</a-select-option>
          </a-select>
          <a-button icon="plus" @click="addUser" >新增</a-button>
        </template>
      </a-table>
    </a-form>

  </div>
</template>

<script>
  import { getDealtownUserList,deleteDealtownUser,addDealtownUser } from '@/api/business'
  import RSwitch from '@/components/RSwitch/'
  import UserPicker from '@/components/UserPicker/'
  export default {
    components: {
      RSwitch,
      UserPicker
    },
    data () {
      return {
        saveLoading:false,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        form: this.$form.createForm(this),

        //人员管理表头
        userColumns: [
          {
            title: '#',
            dataIndex: 'id',
            width: '80px'
          },
          {
            title: '用户名',
            dataIndex: 'name',
            scopedSlots: { customRender: 'name' },
          },
          {
            title: '角色',
            width:'100px',
            dataIndex: 'role_type',
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '150px',
            scopedSlots: { customRender: 'action' },
          }
        ],
        businessUserList:[],

        userFormData:{
          user_id:undefined,
          type: undefined,
        },
      }
    },
    mounted(){
      let that = this;
      that.fetch();
    },
    methods: {
      fetch(){
        let that = this;
        getDealtownUserList().then(res => {
          that.businessUserList = res.data.list
        })

      },
      addUser(){
        let self = this;
        if(!self.userFormData.user_id){
          self.$message.error('请选择人员');
          return ;
        }
        if(!self.userFormData.type){
          self.$message.error('请选择类型');
          return ;
        }
        console.log(self.userFormData)
        addDealtownUser(self.userFormData).then(res => {
          self.userFormData.user_id = undefined;
          self.userFormData.type = undefined;
          self.$message.success('新增成功')
          self.fetch();
        }).catch(err =>{
          self.$message.error(err.data.msg);
        })
      },
      deleteUser(record,index){
        let self = this;
        deleteDealtownUser({
          user_id:record.user_id,
        }).then(res => {
          self.$message.success('删除成功')
          self.businessUserList.splice(index,1);
        }).catch(err =>{
          self.$message.error(err.data.msg);
        })
      },
    }
  }
</script>

<style lang="less" scoped>

  .avatar-upload-wrapper {
    height: 200px;
    width: 100%;
  }

  .ant-upload-preview {
    position: relative;
    margin: 0 auto;
    width: 100%;
    max-width: 180px;
    border-radius: 50%;
    box-shadow: 0 0 4px #ccc;

    .upload-icon {
      position: absolute;
      top: 0;
      right: 10px;
      font-size: 1.4rem;
      padding: 0.5rem;
      background: rgba(222, 221, 221, 0.7);
      border-radius: 50%;
      border: 1px solid rgba(0, 0, 0, 0.2);
    }
    .mask {
      opacity: 0;
      position: absolute;
      background: rgba(0,0,0,0.4);
      cursor: pointer;
      transition: opacity 0.4s;

      &:hover {
        opacity: 1;
      }

      i {
        font-size: 2rem;
        position: absolute;
        top: 50%;
        left: 50%;
        margin-left: -1rem;
        margin-top: -1rem;
        color: #d6d6d6;
      }
    }

    img, .mask {
      width: 100%;
      max-width: 180px;
      height: 100%;
      border-radius: 50%;
      overflow: hidden;
    }
  }
</style>